<template>
  <div>
    <header>
      <div>
        <span @click="Return" class="jiantou"></span>
        <p>搜索</p>
      </div>
    </header>
    <main>
      <input v-model="value" type="text" placeholder="请输入商家或美食名称" />
      <button @click="add">提交查询</button>
    </main>

    <footer v-show="arr9.length == 0">
      <div class="yanse"></div>
      <p class="lishi">搜索历史</p>
      <div class="bd" v-for="(item, index) in arr" :key="index">
        <span @click="change(item)">
          {{ item }}
        </span>
        <button @click="Delect(index)">删除</button>
      </div>
      <div class="qk" v-if="arr.length > 0" @click="DelectAll">
        清空搜索历史
      </div>
    </footer>
    <footer v-show="arr9.length > 0">
      <p class="shangj">商家</p>
      <div class="hezi" v-for="(value, index) in arr9" :key="index">
        <p>
          {{ value.name }}
        </p>
      </div>
    </footer>
    <divfooter> </divfooter>
  </div>
</template>
<style scoped>
*{
  font-size: 0.3rem;
}
.shangj {
  width: 100%;
  height: 0.8rem;
  line-height: 0.8rem;
  background: #f4f4f4;
  color: #ccc;
  padding-left: 0.4rem;
  box-sizing: border-box;
  font-size: 0.3rem;
}
.hezi {
  font-size: 0.3rem;
}
.qk {
  width: 100%;
  height: 1rem;
  text-align: center;
  line-height: 1rem;
  color: #3190e8;
  font-weight: 700;
  font-size: 0.4rem;
}
.lishi {
  font-size: 0.3rem;
  width: 100%;
  height: 1.4rem;
  padding-left: 0.4rem;
  line-height: 1.4rem;
  box-sizing: border-box;
}
* {
  margin: 0;
  padding: 0;
}
footer {
  flex: 1;
  width: 100%;
  background: #f4f4f4;
  position: relative;
}
.yanse {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -20;
  width: 100%;
  height: 10rem;
  background: #f4f4f4;
}
header {
  position: fixed;
  z-index: 1990;
  top: 0;
  left: 0;
  width: 100%;
  height: 1rem;
  background-color: #3190e8;
}
header div {
  padding-left: 0.2rem;
  padding-right: 0.2rem;
  font-size: 0.3rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 60%;
  height: 1rem;
  color: #fff;
  box-sizing: border-box;
  font-weight: 700;
}
.jiantou {
  width: 0.24rem;
  height: 0.24rem;
  fill: #fff;
  border: 1px solid #fff;
  border-width: 0.06rem 0.06rem 0px 0px;
  transform: rotateZ(225deg);
}
input {
  width: 60%;
  height: 0.8rem;
  padding: 0.3rem;
  box-sizing: border-box;
  margin-left: 0.1rem;
  font-weight: 700;
  border: 0 solid #000;
  background: #f4f4f4;
  border-radius: 4px;
  vertical-align: middle;
}
main {
  height: 2rem;
  margin-top: 0.8rem;
  background: #fff;
  display: flex;
  align-items: center;
}
button {
  height: 0.8rem;
  width: 30%;
  background: #3190e8;
  color: #fff;
  font-weight: 700;
  border-radius: 6px;
  vertical-align: middle;
}
footer div {
  background: #fff;
}
.bd {
  box-sizing: border-box;
  padding-left: 10%;
  padding-right: 10%;
  height: 0.8rem;
  line-height: 0.8rem;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  font-size: 0.26rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
<script>
import divfooter from "../elm-footer/elm-footer.vue";
import { getsearch } from "../../api/index.js";
export default {
  components: {
    divfooter,
  },
  data() {
    return {
      value: "",
      arr: [],
      search: "",
      arr9: [],
    };
  },
  mounted() {
    if (this.$route) {
      this.search = this.$route.query.search;
      console.log(this.$route.query.search);
    }
    if (localStorage.arr) {
      this.arr = JSON.parse(localStorage.arr4);
    }
  },
  methods: {
    /* 点击添加 */
    async add() {
      if (this.arr.indexOf(this.value) == -1) {
        this.arr.push(this.value);
        localStorage.arr4 = JSON.stringify(this.arr);
      }
      var res = await getsearch(this.value, this.search);
      if (res.data) {
        // console.log(res.data);
        this.arr9 = res.data;
      }
    },
    /* 删除 */
    Delect(index) {
      this.arr.splice(index, 1);
      if (localStorage.arr4) {
        localStorage.arr4 = JSON.stringify(this.arr);
      }
    },
    /* 返回 */
    Return() {
      this.$router.go(-1);
    },
    /* 删除全部 */
    DelectAll() {
      this.arr = [];
      if (localStorage.arr4) {
        localStorage.arr4 = JSON.stringify(this.arr);
      }
    },
    /* 点击更改value */
    change(item) {
      this.value = item;
    },
  },
};
</script>
